<?php
include_once("../../../common/module/checkA.php");
include_once("../../../../config.php");

//设置需要搜索的表格的前缀后缀
 $table_suffix = "member";
 $table        = $table_Prefix . $table_suffix;

$now_member=select_one_line_all_items($table_Prefix."member","nid = '".$_REQUEST ['nid']."'");


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <?php include_once(ROOT_PATH_ . '/controlbase/modules/head.php'); ?>
</head>

<body>


<?php
//接收需要修改的数据的id
$new_detail_nid          = addslashes($_REQUEST['nid']);


//传递过来的页面信息。返回的时候，会根据这些信息返回原来的页面
$nowpageid = addslashes($_REQUEST['nowpageid']);
$sortby    = addslashes($_REQUEST['sortby']);
$term_nid  = addslashes($_REQUEST['term_nid']);
$navitemed  = addslashes($_REQUEST['navitemed']);



$_REQUEST['ai_authur']      = $_SESSION['username'];
$_REQUEST['ai_ip']          = $_SERVER["REMOTE_ADDR"];

$_REQUEST['ai_tag']=str_replace("；",";",$_REQUEST['ai_tag']);




//这里是涉及上传文件的部分：包含图片和附件。视频也应该用这种形式上传
//20220315同时把旧的文件，移动到delete中。
//统一设置一下保存图片的路径
$_uf['path']        = ROOT_PATH_ . "/uploadfiles/member/";

//创建删除文件存放文件夹
if(!file_exists($_uf['path'] ."/delete/")){
    mkdir($_uf['path'] ."/delete/",0777,true);
    chmod($_uf['path'] ."/delete/",0777);
}


//上传icon
if (is_uploaded_file($_FILES['icon']['tmp_name']))
{
    $_uf['upfile']       = $_FILES['icon'];
    $_info               = upload_file($_uf);
    $_REQUEST['ai_icon'] = $_info['name'];
    if(($_REQUEST['ai_icon'])&&($now_member['icon']!="alpha.gif"))
    {
        rename($_uf['path'].$now_member['icon'], $_uf['path'] ."delete/".$now_member['icon']);
    }
}


$sql  = update_date($table,$_REQUEST,"nid",$new_detail_nid);
$query = mysql_query($sql) or die ("error:".mysql_error());








//这里处理一下跟踪记录部分的内容
if($_REQUEST['member_log_content'])
{

    $member_log['ai_authur']      = $_SESSION['username'];
    $member_log['ai_ip']          = $_SERVER["REMOTE_ADDR"];
    $member_log['ai_name']        = $_REQUEST['member_log_title'];
    $member_log['ai_contact_way'] = $_REQUEST['member_log_contact_way'];
    $member_log['ai_content']     = $_REQUEST['member_log_content'];
    $member_log['ai_excutor']     = $_SESSION['nid'];
    $member_log['ai_membernid']   = $_REQUEST['nid']; //这个request 极度容易会在页面中被修改成其他id；目前处理方法就是顶部鉴权
    $member_log['ai_unitnid']     = $_REQUEST['ai_unit'];//这个request 极度容易会在页面中被修改成其他id；目前处理方法就是顶部鉴权

    $sql=insert_date($table_prefix."member_contact_log",$member_log,"nid",$new_detail_nid);
    $query = mysql_query($sql) or die ("error:".mysql_error().$sql);







//期限外联系的话，则更新checkpoint。
//在周期内天数（未触及临近期的），则不更新checkpoint




//例如我3号要联系，我四号联系了，那么就更新到次月3号。
//我四号再联系一次，因为未临近次元三号，所以不更新。
//我五号再次联系，因为上次留言是四号，这次留言只差距一天，所以不更新checkpoint。

//为了避免短时间内多次留言，刷新联系时间的情况，不同的checkpoint会有不同的刷新时间点
//每日更新的话，必须在不同一日
//每周更新的话，必须在三日内联系
//每月更新，必须在7天附近
//每年更新，必须在30天附近



    //先判断一下现在的时间是否和checkpoint有特定的差距，看是否需要刷新checkpoint
    //这部分好像没用，就只有echo可以作为输出，看看数据。

//    if( $now_member['period']=='无限制'){$new_checkpoint="";};
//    if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 day"  ,strtotime($now_member['checkpoint'])));}
//    if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint'])  < 3 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 week" ,strtotime($now_member['checkpoint'])));}
//    if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint'])  < 7 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 month",strtotime($now_member['checkpoint'])));}
//    if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint'])  < 30 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 year" ,strtotime($now_member['checkpoint'])));}
//
//
    echo "重复周期".$now_member['period'];
    echo "<br/>";

    echo "时间点".$now_member['checkpoint'];
    echo "<br/>";


    echo "当前限期超时".count_days(date('Y-m-d'),$now_member['checkpoint']);
    echo "<br/>";
//
//
//    echo "新时间".$new_checkpoint;
//    echo "<br/>";
//    echo "<br/>";
//    echo "<br/>";




    if(date('Y-m-d')>$now_member['checkpoint'])
    {
        echo "已经超时";
        echo "<br/>";
        echo "<br/>";

        for($i=1;date('Y-m-d')>=$new_checkpoint;$i++)
        {

            if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i day"  ,strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 3 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i week" ,strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 7 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i month",strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 30 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i year" ,strtotime($now_member['checkpoint'])));}

            echo "循环内的新时间";
            echo $new_checkpoint;
            echo "<br/>";
            echo "<br/>";
        }

    }
    else
    {
        echo "未超时,必须要在checkpoint以后的几天联系才能更新时间";
        echo count_days(date('Y-m-d'),$now_member['checkpoint']);
        echo "<br/>";

        if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) == 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 day"  ,strtotime($now_member['checkpoint'])));}
        if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 6 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 week" ,strtotime($now_member['checkpoint'])));}
        if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 23 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 month",strtotime($now_member['checkpoint'])));}
        if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 335 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 year" ,strtotime($now_member['checkpoint'])));}

    }


    if($new_checkpoint)
    {
        $newcheckpoint['ai_checkpoint']=$new_checkpoint;
        $sql=update_date($table_prefix."member",$newcheckpoint,"nid",$new_detail_nid);
        $query = mysql_query($sql) or die ("error:".mysql_error());
    }




}





?>


<script type="text/javascript">

    alert("输入信息已保存，点击确认返回继续操作.");
    document.location.href = "detail-list.php?nowpageid=<? echo $_GET["nowpageid"] ?>&sortby=<? echo $sortby?>&nid=<? echo $term_nid ?>&sys_part=<? echo $system_part ?>&nav-itemed=<? echo $_GET["nav-itemed"] ?>"

</script>


</body>